package logic

import (
	"context"
	"io"
	"net/http"
	"os"
	"path"

	"imagesearch/internal/svc"
	"imagesearch/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchItemLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	req    *http.Request
}

func NewSearchItemLogic(req *http.Request, svcCtx *svc.ServiceContext) *SearchItemLogic {
	return &SearchItemLogic{
		Logger: logx.WithContext(req.Context()),
		svcCtx: svcCtx,
		req:    req,
	}
}

func (logic *SearchItemLogic) SearchItem() (resp []types.Item, err error) {

	/*	var name = logic.req.FormValue("name")
		var des = logic.req.FormValue("des")*/
	file, handler, err := logic.req.FormFile("image_name")
	if err != nil {
		return nil, err
	}
	defer file.Close()

	tmpFilePath := path.Join(logic.svcCtx.Config.Path, "search", handler.Filename)
	tempFile, err := os.Create(tmpFilePath)
	if err != nil {
		return nil, err
	}
	defer tempFile.Close()
	io.Copy(tempFile, file)
	logx.Infof("File name:%+v", tempFile.Name())

	resp = SearchVearch(logic.svcCtx.Config.VearchConfig, handler.Filename)

	return resp, nil

}
